# Maintainer: Thore Bödecker <foxxx0@archlinux.org>
# Contributor: Yardena Cohen <yardenack at gmail dot com>

pkgname=amavisd-new
_pkgname=amavis
pkgver=2.12.2
pkgrel=1
pkgdesc="High-performance interface between mailer (MTA) and content checkers"
arch=('any')
url="https://gitlab.com/amavis/amavis"
license=('GPL')
depends=(
    'perl>=5.8.2'
    'perl-archive-zip>=1.14'
    'perl-convert-tnef'
    'perl-convert-uulib>=1.4-5'
    'perl-mime-tools'
    'perl-mailtools>=1.58'
    'perl-net-libidn'
    'perl-net-server>=0.88'
    'perl-io-socket-inet6'
    'perl-io-stringy'
    'perl-unix-syslog>=1.1-4'
    'perl-mail-dkim>=0.31'
    'perl-berkeleydb>=0.42'
    'perl-json'
    'perl-http-message'
    'perl-libwww'
    'perl-lwp-protocol-https'
    'perl-net-ssleay'
    'bzip2'
    'gzip'
)
optdepends=(
    'arj:        Decoder for: .arj .exe'
    'lrzip:      Decoder for: .lrz'
    'lz4:        Decoder for: .lz4'
    'lzo:        Decoder for: .lzo'
    'p7zip:      Decoder for: .7z'
    'unrar:      Decoder for: .rar'
    'rpmextract: Decoder for: .rpm'
)
backup=('etc/amavisd/amavisd.conf' 'etc/amavisd/amavisd-custom.conf')
source=("${url}/-/archive/v${pkgver}/amavis-v${pkgver}.tar.bz2"
        "amavisd.service"
        "amavisd.sysusers"
        "amavisd.tmpfiles")
sha512sums=('8a6ab4441a9ef2ee757c839f656bc16f00dd9213f6cc0bb976d7f2b76793adf023d33a42099f07b0dae13281171db9ba0405028882a86e2230126e8d6a29a4ff'
            '5028913479808c02a74682987ed5289d094c1ae2e37874b8c45a6abcc825f2e9d9a46acf9ee010897a5a41e5f9256507cc4d7b9e535c46b03cc06b560dddd908'
            '8933d8457989c949845c0dabd2bed58680adbf87c920f05a59fd136acdd6da84b4d5b48c4c71dfbc0b047be005a70f665138d6397c1b09e0c746903c1d19fc11'
            'bedbd1b270ccbfbbc9ef5682cc9491d6e7895b8b6cb7634a84175c0f469373f325fd694296a9a7aa8c1f09b6571162066e3fc98c555a4d971216fc6721842c9d')

prepare() {
    sed -i \
        -e "s/# @bypass_virus_checks_maps = (1)/@bypass_virus_checks_maps = (1)/g" \
        -e "s/# @bypass_spam_checks_maps  = (1)/@bypass_spam_checks_maps = (1)/g" \
        -e "s/\\\$daemon_user  = 'vscan'/\\\$daemon_user  = 'amavis'/g" \
        -e "s/\\\$daemon_group = 'vscan'/\\\$daemon_group = 'amavis'/g" \
        -e "sX# \\\$MYHOME = '/var/amavis'X\\\$MYHOME = '/var/spool/amavis'Xg" \
        -e "sX\\\$QUARANTINEDIR = '/var/virusmails'X\\\$QUARANTINEDIR = \"\\\$MYHOME/virus\"Xg" \
        -e "sX# \\\$pid_file  = \"\\\$MYHOME/var/amavisd.pid\"X\\\$pid_file = \"/run/amavis/pid\"Xg" \
        -e "sX# \\\$lock_file = \"\\\$MYHOME/var/amavisd.lock\"X\\\$lock_file = \"/run/amavis/lock\"Xg" \
        -e "sX\\\$unix_socketname = \"\\\$MYHOME/amavisd.sock\"X\\\$unix_socketname = \"/run/amavis/sock\"Xg" \
        "${srcdir}/${_pkgname}-v${pkgver}/amavisd.conf"

    sed -i \
        -e "sX\\\$socketname = '/var/amavis/amavisd.sock'X\\\$socketname = '/var/spool/amavis/amavisd.sock'Xg" \
        "${srcdir}/${_pkgname}-v${pkgver}/amavisd-release"
}

package() {
    cd "${srcdir}/${_pkgname}-v${pkgver}"
    install -D -m755 JpegTester.pm               "${pkgdir}/usr/share/perl5/vendor_perl/JpegTester.pm"
    install -D -m644 LDAP.ldif                   "${pkgdir}/etc/openldap/schema/amavisd-new.ldif"
    install -D -m644 LDAP.schema                 "${pkgdir}/etc/openldap/schema/amavisd-new.schema"
    install -D -m750 -o 333 -g 333 -d            "${pkgdir}/var/spool/amavis/"
    install -D -m750 -o 333 -g 333 -d            "${pkgdir}/var/spool/amavis/db"
    install -D -m750 -o 333 -g 333 -d            "${pkgdir}/var/spool/amavis/tmp"
    install -D -m750 -o 333 -g 333 -d            "${pkgdir}/var/spool/amavis/var"
    install -D -m750 -o 333 -g 333 -d            "${pkgdir}/var/spool/amavis/virus"

    install -D -m644 -t "${pkgdir}/etc/amavisd"             amavisd{.conf,-custom.conf,.conf-default}
    install -D -m644 -t "${pkgdir}/usr/share/doc/amavis"    amavisd-new-{qmqpqq,courier}.patch
    install -D -m755 -t "${pkgdir}/usr/bin"                 p0f-analyzer.pl amavisd{,-{agent,nanny,release,signer,snmp-subagent{,-zmq}}}

    install -D -m644 "${srcdir}/amavisd.service"  "${pkgdir}/usr/lib/systemd/system/amavisd.service"
    install -D -m644 "${srcdir}/amavisd.sysusers" "${pkgdir}/usr/lib/sysusers.d/amavisd.conf"
    install -D -m644 "${srcdir}/amavisd.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/amavisd.conf"
}
